package com.winspread.mvvm.base.vm


import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import com.alibaba.android.arouter.launcher.ARouter
import com.gyf.immersionbar.ktx.immersionBar
import com.winspread.mvvm.base.m.BaseViewModel
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type


open class BaseActivity<VM : BaseViewModel, ViewDataBinding : androidx.databinding.ViewDataBinding?>(
    layoutId: Int
) : AppCompatActivity() {

    val viewModel by lazy {
//        ViewModelProviders.of(this)
//            .get(getClass() as Class<VM>)
        ViewModelProvider(this).get(getClass()as Class<VM>)
    }

    val binding: ViewDataBinding by lazy {
        DataBindingUtil.setContentView<ViewDataBinding>(
            this,
            layoutId
        )
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initBar()
        ARouter.getInstance().inject(this)
        binding?.lifecycleOwner = this
        requestPermission();
        initView()
        initData(intent.extras)
        // oberver 统一处理
        onOberver()
        // 统一监听事件
        initListenter()
    }



    var statusBarFont = false
    var isBar = true




    /**
     *
     * 默认沉浸式，状态栏文字白色
     *
     * 实现类需要改变，如：沉浸式 ，黑色字体
     *
     * 实现类
     *
     * init{
     *  initImmersionBar(statusBarFont = true, isBar = true)
     * }
     *
     *
     * @param statusBarFont Boolean  状态栏字体 false 白色 ture 黑色
     *
     * @param isBar Boolean  是否沉浸式
     */
    fun initImmersionBar(statusBarFont: Boolean = false, isBar: Boolean = true) {
        this.statusBarFont = statusBarFont
        this.isBar = isBar
    }


    private fun initBar() {
        if (isBar) {
            immersionBar {
                transparentBar()
                statusBarDarkFont(statusBarFont)//状态栏字体颜色
                titleBar(binding?.root)

            }
        }
    }

    open fun requestPermission() {}


    private fun getClass(): Type? {
        return (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<VM?>
    }

    //数据处理
    open fun initData(extras: Bundle?) {}

    //界面
    open fun initView() {

    }
    open fun onOberver() {

    }

    open fun initListenter() {

    }

}
